/**
 * 对路由的操作方法，防止数据外泄
 * manipulatingRoute
 * controlRoute
 * staticRout   :   {}
    dynamicRout  :   {}
    // 特殊接口 如400 public
    specialRoute :   {}
    // 解析路由时临时存储
    parsStaticRoute:   {}
    parsDynamicRout:  {}
 */
// 引入工厂
import RouteClusterStructure from '../../model/RouteClusterStructure';

//全局路由对象
if(global.$routeCluster === undefined){
    global.$routeCluster = new RouteClusterStructure();
} 

let controRoute = class {

    // 添加中间件到中间件池
    static addMiddleware(id:string,c:controllerStructure):void{
        global.$routeCluster.MiddlewarePool[id] = c;
    }
    // 获取中间件
    static getMiddleware(id:string):controllerStructure{
        return global.$routeCluster.MiddlewarePool[id];
    }
    // 删除中间件池中的中间件
    static deleteMiddleware(id:string):void{
        delete global.$routeCluster.MiddlewarePool[id];
    }

    // 添加全局中间件
    static addGlobalMiddleware(data:any){
        global.$routeCluster.globalMiddlewarePool.push(data);
    }
    // 设置全局中间件
    static setGlobalMiddleware(data:any){
        if(!Array.isArray(data)) {throw new Error("is data not array")}
        global.$routeCluster.globalMiddlewarePool = data;
    }
    // 获取全局中间件
    static getGlobalMiddleware():any{
        return global.$routeCluster.globalMiddlewarePool;
    }
    // 添加插件
    static addPlug(plug:controllerStructure){
        return global.$routeCluster.plugPool.push(plug)-1;
    }
    // 获取插件
    static getPlug(id?:any) :Array<controllerStructure>|controllerStructure{
        if(id !== undefined)
        return global.$routeCluster.plugPool[id];
        return global.$routeCluster.plugPool;
    }


    // 获取静态路由
    static getStaticRoute(type?:string) :routeTypeStructure|routeStructure{
        if(type !== undefined)
        return global.$routeCluster.staticRout[type];
        return global.$routeCluster.staticRout;
    }
    // 获取动态路由
    static getDynamicRoute(type?:string) :routeTypeStructure|routeStructure{
        if(type !== undefined)
        return global.$routeCluster.dynamicRout[type];
        return global.$routeCluster.dynamicRout;
    }

    // 设置动态路由
    static setDynamicRoute(route:routeStructure) :void{
        global.$routeCluster.dynamicRout = route;
    }
    // 设置静态路由
    static setStaticRoute(route:routeStructure) :void{
        global.$routeCluster.staticRout = route;
    }

    // 获取未处理的静态路由
    static getUnStaticRoute(id?:string) :unrouteStructure|routeStructure{
        if(id)
        return global.$routeCluster.unprocessedStaticRout[id];
        return global.$routeCluster.unprocessedStaticRout;
    }
    // 获取未处理的动态路由
    static getUnDynamicRoute(id?:string) :unrouteStructure|routeStructure{
        if(id)
        return global.$routeCluster.unprocessedDynamicRout[id];
        return global.$routeCluster.unprocessedDynamicRout;
    }

    // 添加动态路由
    static addStaticRouteInter(type:string,url:string,route:routeInterface):void{
        global.$routeCluster.staticRout[type][url] = route;
    }
    // 添加静态路由
    static addDynamicRouteInter(type:string,url:string,route:routeInterface):void{
        global.$routeCluster.dynamicRout[type][url] = route;
    }

    // 获取动态路由接口
    static getStaticRouteInter(type:string,url:string):routeInterface{
        return global.$routeCluster.staticRout?.[type]?.[url];
    }
    // 获取静态路由接口
    static getDynamicRoutInter(type:string,url:string):routeInterface{
        return global.$routeCluster.dynamicRout?.[type]?.[url];
    }

    // 添加未处理一个文件的路由 id为文件的路径
    static addUnStaticRoute(id:string,route:routeStructure):void{
        global.$routeCluster.unprocessedStaticRout[id] = route;
    }
    // 添加未处理文件路由到动态解析路由中
    static addUnDynamicRoute(id:string,route:routeStructure):void{
        global.$routeCluster.unprocessedDynamicRout[id] = route;
    }

    // 添加一个未处理的静态接口
    static addUnStaticRouteInter(id:string,type:string,url:string,route:routeInterface){
        global.$routeCluster.unprocessedStaticRout[id][type][url] = route;
    }

    // 添加一个未处理的动态接口
    static addUnDynamicRouteInter(id:string,type:string,url:string,route:routeInterface){
        global.$routeCluster.unprocessedDynamicRout[id][type][url] = route;
    }

    // 获取未处理的静态路由接口
    static getUnStaticRouteInter(id:string,type:string,url:string) :routeInterface{
        return global.$routeCluster.unprocessedStaticRout[id][type][url];
    }

    // 获取未处理的动态路由接口
    static getUnDynamicRouteInter(id:string,type:string,url:string) :routeInterface{
        return global.$routeCluster.unprocessedDynamicRout[id][type][url];
    }

    // 添加特殊路由
    static addSpecialRoute(id:string,route:routeInterface):void{
        global.$routeCluster.specialRoute[id] = route;
    }


    // 更改特殊接口
    static setSpecialRoute(id:string,route:routeInterface):void{
        this.addSpecialRoute(id,route);
    }


    // 获取特殊接口
    static getSpecialRoute(id?:string):Record<string,routeInterface>|routeInterface{
        if(id)
        return global.$routeCluster.specialRoute[id];
        return global.$routeCluster.specialRoute;
    }
}

if(module) module.exports = controRoute;
export default controRoute;